package com.manxi.controller.user;

import com.manxi.dto.OrdersPageQueryDTO;
import com.manxi.dto.OrdersPaymentDTO;
import com.manxi.dto.OrdersSubmitDTO;
import com.manxi.entity.OrderDetail;
import com.manxi.result.PageResult;
import com.manxi.result.Result;
import com.manxi.service.OrderService;
import com.manxi.vo.OrderPaymentVO;
import com.manxi.vo.OrderSubmitVO;
import com.manxi.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController("userOrderController")
@RequestMapping("/user/order")
@Slf4j
@Api(tags = "C端-订单接口")
public class OrderController {

    @Autowired
    private OrderService orderService;

//    /**
//     * 用户下单
//     *
//     * @param ordersSubmitDTO
//     * @return
//     */
//    @PostMapping("/submit")
//    @ApiOperation("用户下单")
//    public Result<OrderSubmitVO> submit(@RequestBody OrdersSubmitDTO ordersSubmitDTO) {
//        log.info("用户下单:{}", ordersSubmitDTO);
//        OrderSubmitVO orderSubmitVO = orderService.submitOrder(ordersSubmitDTO);
//        return Result.success(orderSubmitVO);
//    }


    /**
     * 订单支付
     *
     * @param ordersPaymentDTO
     * @return
     */
    @PostMapping("/payment")
    @ApiOperation("订单支付")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        log.info("订单支付：{}", ordersPaymentDTO);
        OrderPaymentVO orderPaymentVO = orderService.payment(ordersPaymentDTO);
        log.info("生成预支付交易单：{}", orderPaymentVO);
        return Result.success(orderPaymentVO);
    }

    /**
     * 查询历史订单
     *
     * @param ordersPageQueryDTO
     * @return
     */
    @GetMapping("/historyOrders")
    @ApiOperation("查询历史订单")
    public Result<PageResult> page(OrdersPageQueryDTO ordersPageQueryDTO) {
        log.info("查询历史订单:{}", ordersPageQueryDTO);
        PageResult pageResult = orderService.pageQuery(ordersPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 查询订单详情
     * @param id
     * @return
     */
    @GetMapping("/orderDetail/{id}")
    @ApiOperation("查询订单详情")
    public Result<OrderVO> details(@PathVariable Long id) {
        log.info("订单id:{}", id);
        OrderVO orderVO = orderService.details(id);

        if(orderVO.getOrderType() == 0 || orderVO.getOrderType() == 1){
            String substring = orderVO.getPickupNumber().substring(orderVO.getPickupNumber().length() - 2);
            orderVO.setPickupNumber(substring);
        }
        return Result.success(orderVO);
    }


    /**
     * 用户取消订单
     * @param id
     * @return
     */
    @PutMapping("/cancel/{id}")
    @ApiOperation("用户取消订单")
    public Result cancel(@PathVariable Long id){
        log.info("用户取消订单的id:{}",id);
        orderService.userCancelById(id);
        return Result.success();
    }

    /**
     * 再来一单
     * @param id
     * @return
     */
    @PostMapping("/repetition/{id}")
    @ApiOperation("再来一单")
    public Result repetition(@PathVariable Long id){
        log.info("再来一单订单id:{}",id);
        orderService.repetition(id);
        return Result.success();
    }
    /**
     * 根据id删除订单
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation("根据id删除订单")
    public Result delete(@PathVariable Long id){
        log.info("订单id:{}",id);
        orderService.delete(id);
        return Result.success();
    }

    /**
     * 用户催单
     * @param id
     * @return
     */
    @GetMapping("/reminder/{id}")
    @ApiOperation("用户催单")
    public Result reminder(@PathVariable Long id){
        log.info("催单的订单id:{},",id);
        orderService.reminder(id);
        return Result.success();
    }
}
